package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.util.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(
                        dishId != null,
                        ShoppingCart::getDishId,
                        shoppingCart.getDishId()
                )
                .eq(
                        setmealId != null,
                        ShoppingCart::getSetmealId,
                        shoppingCart.getSetmealId()

                ).eq(
                        ShoppingCart::getUserId,
                        BaseContext.getId()
                );

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if (cart != null) {
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
            return R.success("添加购物车成功");
        }
        shoppingCart.setUserId(BaseContext.getId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartService.save(shoppingCart);
        return R.success("添加购物车成功");
    }


    /**
     * 查询用户购物车
     *
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class).eq(ShoppingCart::getUserId, BaseContext.getId());

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 清空用户购物车
     *
     * @return
     */
    @DeleteMapping("clean")
    public R<String> clean() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class).eq(ShoppingCart::getUserId, BaseContext.getId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 减少购物车某商品数量
     *
     * @return
     */
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {

        //   LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class).eq(ShoppingCart::getUserId, BaseContext.getId());


        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(
                        dishId != null,
                        ShoppingCart::getDishId,
                        shoppingCart.getDishId()
                )
                .eq(
                        setmealId != null,
                        ShoppingCart::getSetmealId,
                        shoppingCart.getSetmealId()

                ).eq(
                        ShoppingCart::getUserId,
                        BaseContext.getId()
                );

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);


        //如果当前数量为1,不再将数量减为0,而是直接删除该条目
        if (cart.getNumber() - 1==0) {
            shoppingCartService.removeById(cart);
        } else {
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartService.updateById(cart);
        }


        return R.success("减少数量成功");
    }

}
